package com.two.screen.water.balance.serial.battery

import com.two.screen.water.balance.listener.MyObserver
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.RxUtils
import com.two.screen.water.balance.utils.ToastUtil
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe

object BatteryAll {

    fun initSerialPortBattery() {
        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                batteryClose()
                SerialPortBatteryZzUtils.initSerialPortBattery()
            } catch (exc: Exception) {
                d("初始称重失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    fun batteryClose() {
        try {
            SerialPortBatteryZzUtils.close()
        } catch (e: Exception) {
            d("关闭称重失败：$e")
        }
    }

    fun d(msg: String) {
        CommUtils.println("BatteryAll->", msg)
    }
}